<?php

/**
 * Class PayCallback 回调函数
 */
namespace V1;
class PayCallback extends \ResourceClass{
    public $isActive = true;
    public $remark = '回调处理函数';

    public function post(array $req, array $post, &$result){
       $aliPayData = $this->AliPayCallback($post);
       exit;

    }
    public function AliPayCallback($post){
        if(empty($post)){
          echo "fail";return;
        }
        //商户订单号
        $out_trade_no =$post['out_trade_no'];//'5100000001201610272013315043';//$_POST['out_trade_no'];
        //支付宝交易号
        $trade_no =$post['trade_no'];//'2016072021001004910283167622'; //$post['trade_no']; $post['trade_no'];
        //交易状态
        $trade_status =$post['trade_status'];//'TRADE_FINISHED'; //$post['trade_status']; $post['trade_status'];
        $transaction_handler = new \Biz\TransactionHandler();
        $order_handler       = new \Biz\OrderHandler();
        $tabulation          = new \Cls\Tabulation();
        if($trade_status=='TRADE_FINISHED'){
            //1.根据订单号查询交易状态值
            $tabulation->setWhere('out_trade_no',$out_trade_no);
            $tabulation->setWhere('status',\Cls\Transaction\Transaction::NEW_TRANSACTION_STATUS);
            $tabulation->setWhere('platform',\Cls\Transaction\Transaction::PAY_ALIPAY);
            $transaction_handler->getTransactionByOrderId($tabulation);
            $transaction_items = $tabulation->getData();
            $transaction_cls   = reset($transaction_items);
            if($transaction_cls instanceof \Cls\Transaction\Transaction){
                $orderId = $transaction_cls->getReference();
                $transaction_id = $transaction_cls->getId();
                //修改transaction status,字段,platform_id 交易字段
                $transaction_cls->setStatus(\Cls\Transaction\Transaction::OK_TRANSACTION_STATUS);
                $transaction_cls->setPlatformId($trade_no);
                $update_transaction_cls = $transaction_handler->update($transaction_cls);
                if($update_transaction_cls){
                    //查询是否有该订单是否需要修改
                    $order_cls = $order_handler->getOrderById($orderId);
                    if($order_cls instanceof \Cls\Order\Order){
                        $status = $order_cls->getStatus();
                        $pay_status = $order_cls->getPayStatus();
                        if(($status==\Cls\Order\Order::ORDER_STATUS_NEW)&&($pay_status==\Cls\Order\Order::PAY_STATUS_NOT)){
                            //订单条件满足则修改
                            $order_cls->setPayStatus(\Cls\Order\Order::PAY_STATUS_HAD);
                            $order_cls->setTransactionId($transaction_id);
                            $order_cls->setIsPayType(\Cls\Order\Order::IS_PAY_TYPE_ONLINE);
                            $order_cls->setPayTime(date('Y-m-d H:i:s'));
                            $order_cls->setPayClassType('支付宝支付');
                            $update_order_cls = $order_handler->update($order_cls);
                            if($update_order_cls){
                                //todo edit solr
                                $solr_order_cls = $order_handler->getSolrOrderByOrderId($orderId);
                                if($solr_order_cls instanceof \Cls\Order\SolrOrder){
                                    $solr_order_cls->setPayStatus(\Cls\Order\Order::PAY_STATUS_HAD);
                                    $solr_update = $order_handler->updateSolrOrder($solr_order_cls);
                                    echo "success";
                                    //file_put_contents(__DIR__.'/../log/STATUS_TRADE_FINISHED'.date('YmdHis').'.txt','TRADE_FINISHED');
                                }
                            }
                        }
                    }
                }
            }
        }elseif($trade_status=='TRADE_SUCCESS'){
            //1.根据订单号查询交易状态值
            $tabulation->setWhere('out_trade_no',$out_trade_no);
            $tabulation->setWhere('status',\Cls\Transaction\Transaction::NEW_TRANSACTION_STATUS);
            $tabulation->setWhere('platform',\Cls\Transaction\Transaction::PAY_ALIPAY);
            $transaction_handler->getTransactionByOrderId($tabulation);
            $transaction_items = $tabulation->getData();
            $transaction_cls   = reset($transaction_items);
            if($transaction_cls instanceof \Cls\Transaction\Transaction){
                $orderId = $transaction_cls->getReference();
                $transaction_id = $transaction_cls->getId();
                //修改transaction status,字段,platform_id 交易字段
                $transaction_cls->setStatus(\Cls\Transaction\Transaction::OK_TRANSACTION_STATUS);
                $transaction_cls->setPlatformId($trade_no);
                $update_transaction_cls = $transaction_handler->update($transaction_cls);
                if($update_transaction_cls){
                    //查询是否有该订单是否需要修改
                    $order_cls = $order_handler->getOrderById($orderId);
                    if($order_cls instanceof \Cls\Order\Order){
                        $status = $order_cls->getStatus();
                        $pay_status = $order_cls->getPayStatus();
                        if(($status==\Cls\Order\Order::ORDER_STATUS_NEW)&&($pay_status==\Cls\Order\Order::PAY_STATUS_NOT)){
                            //订单条件满足则修改
                            $order_cls->setPayStatus(\Cls\Order\Order::PAY_STATUS_HAD);
                            $order_cls->setTransactionId($transaction_id);
                            $order_cls->setIsPayType(\Cls\Order\Order::IS_PAY_TYPE_ONLINE);
                            $order_cls->setPayTime(date('Y-m-d H:i:s'));
                            $order_cls->setPayClassType('支付宝支付');
                            $update_order_cls = $order_handler->update($order_cls);
                            if($update_order_cls){
                                //todo edit solr
                                $solr_order_cls = $order_handler->getSolrOrderByOrderId($orderId);
                                if($solr_order_cls instanceof \Cls\Order\SolrOrder){
                                    $solr_order_cls->setPayStatus(\Cls\Order\Order::PAY_STATUS_HAD);
                                    $solr_update = $order_handler->updateSolrOrder($solr_order_cls);
                                    echo "success";
                                    //file_put_contents(__DIR__.'/../log/STATUS_TRADE_FINISHED'.date('YmdHis').'.txt','TRADE_FINISHED');

                                }
                            }
                        }
                    }
                }
            }
        }
        echo "success";
    }
}
?>